#include <pthread.h>
#include <unistd.h>
#include <ctime>

#include "BlockQueue.hpp"

int myAdd(int x, int y)
{
    return x + y;
}

void* consumer(void* args)
{
    BlockQueue* bq = (BlockQueue*)args;
    int data;
    while(1)
    {
        bq->pop(data);
        std::cout << pthread_self() << " Consumer: " << data << std::endl;
    }
    return nullptr;
}

void* productor(void* args)
{
    BlockQueue* bq = (BlockQueue*)args;
    while(1)
    {
        int data=rand()%10 + 1;
        usleep(rand()%1000);
        bq->push(data);
        std::cout << pthread_self() << " Productor: " << data << std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    srand((uint64_t)time(nullptr) ^ getpid() ^ 0x32457);
    BlockQueue* bqueue=new BlockQueue();

    // 单生产者、单消费者
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, bqueue);
    pthread_create(&p, nullptr, productor, bqueue);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bqueue;
    return 0;
}